package com.daidai.activitys

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.accessibility.AccessibilityManager
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.daidai.base.BaseActivity
import com.daidai.spinner.R
import com.daidai.tools.MyLog
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions

class LightServiceActivity : BaseActivity() {
    override fun getContentViewResId(): Int = R.layout.activity_lightservice

    override fun initView() {
        findBtn(R.id.btnSend).setOnClickListener {
            MyLog.info("发送通知,查看led灯的情况!")
            sendNotification()
        }

        findBtn(R.id.btnVibort).setOnClickListener {
            val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator
            if (vibrator != null) {
                MyLog.info("vibrator is not null!")

                // This ignores all exceptions to stay compatible with pre-O implementations.
                vibrator.vibrate(400)
            }
        }

        findBtn(R.id.btnClipText).setOnClickListener {
            val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            val clipData = ClipData.newPlainText("label1","XXXXOOOOOOO!")
            clipboard.setPrimaryClip(clipData)
            MyLog.info("复制了文字")
        }

        findBtn(R.id.btnPaste).setOnClickListener {
            val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            if (clipboard.hasPrimaryClip()){
                val item = clipboard.primaryClip?.getItemAt(0)
                MyLog.info("结果为:${item?.text}")
            }else{
                MyLog.info("毛都没有!")
            }
        }

        findBtn(R.id.btnLife).setOnClickListener {
            startActivity(Intent(this,LifeActivity::class.java))
        }

        // AccessService
        val accMgr = getSystemService(ACCESSIBILITY_SERVICE) as AccessibilityManager
        if (accMgr.isEnabled){
            MyLog.info("AccessService is enable!")
        }else{
            MyLog.info("AccessService is not enable!")
        }
    }

    override fun initData() {
    }

    fun sendNotification() {
        XXPermissions
            .with(this)
            .permission(Permission.NOTIFICATION_SERVICE)
            .request { permissions, all ->
                MyLog.info("可以进行发送了!")
                send()
            }
    }

    fun send() {
        val manager = NotificationManagerCompat.from(applicationContext)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                "resident_channel_id",
                "resident_channel_name",
                NotificationManager.IMPORTANCE_LOW
            )
            manager.createNotificationChannel(channel)
        }

        val notification = NotificationCompat.Builder(this, "resident_channel_id")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)  // 优先级
            .setContentTitle(resources.getString(R.string.app_name))
            .setContentText("健康生活,从天天健走开始.")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setOnlyAlertOnce(true)
            .setLargeIcon(
                BitmapFactory.decodeResource(
                    resources,
                    R.mipmap.ic_launcher
                )
            ) // .setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background)))
            .setNumber(1)
//            .setLights(Color.BLUE,300,300)
            .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
            .setAutoCancel(true).build()
        manager.notify(11, notification)
    }




    //=======================
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MyLog.info("===================LightServiceActivity::onCreate called!================")
    }

    override fun onStart() {
        super.onStart()
        MyLog.info("===================LightServiceActivity::onStart called!================")
    }

    override fun onResume() {
        super.onResume()
        MyLog.info("===================LightServiceActivity::onResume called!================")
    }

    override fun onPause() {
        super.onPause()
        MyLog.info("===================LightServiceActivity::onPause called!================")
    }

    override fun onStop() {
        super.onStop()
        MyLog.info("===================LightServiceActivity::onStop called!================")
    }

    override fun onDestroy() {
        super.onDestroy()
        MyLog.info("===================LightServiceActivity::onDestroy called!================")
    }
}